package com.openbravo.data.user;

import com.openbravo.basic.BasicException;
import com.openbravo.data.loader.LocalRes;
import java.awt.Component;
import java.util.Comparator;
import java.util.EventListener;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.event.EventListenerList;

public class BrowsableEditableData
{
  public static final int ST_NORECORD = 0;
  public static final int ST_UPDATE = 1;
  public static final int ST_DELETE = 2;
  public static final int ST_INSERT = 3;
  private static final int INX_EOF = -1;
  private BrowsableData m_bd;
  protected EventListenerList listeners = new EventListenerList();
  private EditorRecord m_editorrecord;
  private DirtyManager m_Dirty;
  private int m_iState;
  private int m_iIndex;
  private boolean m_bIsAdjusting;
  private boolean iseditable = true;

  public BrowsableEditableData(BrowsableData paramBrowsableData, EditorRecord paramEditorRecord, DirtyManager paramDirtyManager)
  {
    this.m_bd = paramBrowsableData;
    this.m_editorrecord = paramEditorRecord;
    this.m_Dirty = paramDirtyManager;
    this.m_iState = 0;
    this.m_iIndex = -1;
    this.m_bIsAdjusting = false;
    if(  this.m_editorrecord!=null)//ati
    this.m_editorrecord.writeValueEOF();
    this.m_Dirty.setDirty(false);
  }

  public BrowsableEditableData(ListProvider paramListProvider, SaveProvider paramSaveProvider, Comparator paramComparator, EditorRecord paramEditorRecord, DirtyManager paramDirtyManager)
  {
    this(new BrowsableData(paramListProvider, paramSaveProvider, paramComparator), paramEditorRecord, paramDirtyManager);
  }

  public BrowsableEditableData(ListProvider paramListProvider, SaveProvider paramSaveProvider, EditorRecord paramEditorRecord, DirtyManager paramDirtyManager)
  {
    this(new BrowsableData(paramListProvider, paramSaveProvider, null), paramEditorRecord, paramDirtyManager);
  }

  public final ListModel getListModel()
  {
    return this.m_bd;
  }

  public final boolean isAdjusting()
  {
    return (this.m_bIsAdjusting) || (this.m_bd.isAdjusting());
  }

  private final Object getCurrentElement()
  {
    return (this.m_iIndex >= 0) && (this.m_iIndex < this.m_bd.getSize()) ? this.m_bd.getElementAt(this.m_iIndex) : null;
  }

  public final int getIndex()
  {
    return this.m_iIndex;
  }

  public final void addStateListener(StateListener paramStateListener)
  {
    this.listeners.add(StateListener.class, paramStateListener);
  }

  public final void removeStateListener(StateListener paramStateListener)
  {
    this.listeners.remove(StateListener.class, paramStateListener);
  }

  public final void addEditorListener(EditorListener paramEditorListener)
  {
    this.listeners.add(EditorListener.class, paramEditorListener);
  }

  public final void removeEditorListener(EditorListener paramEditorListener)
  {
    this.listeners.remove(EditorListener.class, paramEditorListener);
  }

  public final void addBrowseListener(BrowseListener paramBrowseListener)
  {
    this.listeners.add(BrowseListener.class, paramBrowseListener);
  }

  public final void removeBrowseListener(BrowseListener paramBrowseListener)
  {
    this.listeners.remove(BrowseListener.class, paramBrowseListener);
  }

  public int getState()
  {
    return this.m_iState;
  }

  private void fireStateUpdate()
  {
    EventListener[] arrayOfEventListener = this.listeners.getListeners(StateListener.class);
    int i = getState();
    for (int j = 0; j < arrayOfEventListener.length; j++)
      ((StateListener)arrayOfEventListener[j]).updateState(i);
  }

  protected void fireDataBrowse()
  {
    this.m_bIsAdjusting = true;
    Object localObject = getCurrentElement();
    int i = getIndex();
    int j = this.m_bd.getSize();
    if (localObject == null)
    {
      this.m_iState = 0;
      //ati
      if( this.m_editorrecord!=null)
      this.m_editorrecord.writeValueEOF();
    }
    else
    {
      this.m_iState = 1;
      this.m_editorrecord.writeValueEdit(localObject);
    }
    this.m_Dirty.setDirty(false);
    fireStateUpdate();
    EventListener[] arrayOfEventListener = this.listeners.getListeners(EditorListener.class);
    for (int k = 0; k < arrayOfEventListener.length; k++)
      ((EditorListener)arrayOfEventListener[k]).updateValue(localObject);
    arrayOfEventListener = this.listeners.getListeners(BrowseListener.class);
    for (int k = 0; k < arrayOfEventListener.length; k++)
      ((BrowseListener)arrayOfEventListener[k]).updateIndex(i, j);
    this.m_bIsAdjusting = false;
  }

  public boolean canLoadData()
  {
    return this.m_bd.canLoadData();
  }

  public void setEditable(boolean paramBoolean)
  {
    this.iseditable = paramBoolean;
  }

  public boolean canInsertData()
  {
    return (this.iseditable) && (this.m_bd.canInsertData());
  }

  public boolean canDeleteData()
  {
    return (this.iseditable) && (this.m_bd.canDeleteData());
  }

  public boolean canUpdateData()
  {
    return (this.iseditable) && (this.m_bd.canUpdateData());
  }

  public void refreshCurrent()
  {
    baseMoveTo(this.m_iIndex);
  }

  public void refreshData()
    throws BasicException
  {
    saveData();
    this.m_bd.refreshData();
    this.m_editorrecord.refresh();
    baseMoveTo(0);
  }

  public void loadData()
    throws BasicException
  {
    saveData();
    this.m_bd.loadData();
    //ati
    if( this.m_editorrecord!=null)
    this.m_editorrecord.refresh();
    baseMoveTo(0);
  }

  public void unloadData()
    throws BasicException
  {
    saveData();
    this.m_bd.unloadData();
    this.m_editorrecord.refresh();
    baseMoveTo(0);
  }

  public void sort(Comparator paramComparator)
    throws BasicException
  {
    saveData();
    this.m_bd.sort(paramComparator);
    baseMoveTo(0);
  }

  public void moveTo(int paramInt)
    throws BasicException
  {
    saveData();
    if (this.m_iIndex != paramInt)
      baseMoveTo(paramInt);
  }

  public final void movePrev()
    throws BasicException
  {
    saveData();
    if (this.m_iIndex > 0)
      baseMoveTo(this.m_iIndex - 1);
  }

  public final void moveNext()
    throws BasicException
  {
    saveData();
    if (this.m_iIndex < this.m_bd.getSize() - 1)
      baseMoveTo(this.m_iIndex + 1);
  }

  public final void moveFirst()
    throws BasicException
  {
    saveData();
    if (this.m_bd.getSize() > 0)
      baseMoveTo(0);
  }

  public final void moveLast()
    throws BasicException
  {
    saveData();
    if (this.m_bd.getSize() > 0)
      baseMoveTo(this.m_bd.getSize() - 1);
  }

  public final int findNext(Finder paramFinder)
    throws BasicException
  {
    return this.m_bd.findNext(this.m_iIndex, paramFinder);
  }

  public void saveData()
    throws BasicException
  {
    if (this.m_Dirty.isDirty())
    {
      int i;
      if (this.m_iState == 1)
      {
        i = this.m_bd.updateRecord(this.m_iIndex, this.m_editorrecord.createValue());
        this.m_editorrecord.refresh();
        baseMoveTo(i);
      }
      else if (this.m_iState == 3)
      {
    	  //bd==com.openbravo.data.user.BrowsableData@75f2bf
    	  //com.openbravo.pos.customers.CustomersView===m_editorrecord
        i = this.m_bd.insertRecord(this.m_editorrecord.createValue());
        this.m_editorrecord.refresh();
        baseMoveTo(i);
      }
      else if (this.m_iState == 2)
      {
        i = this.m_bd.removeRecord(this.m_iIndex);
        this.m_editorrecord.refresh();
        baseMoveTo(i);
      }
    }
  }

  public void actionReloadCurrent(Component paramComponent)
  {
    if ((!this.m_Dirty.isDirty()) || (JOptionPane.showConfirmDialog(paramComponent, LocalRes.getIntString("message.changeslost"), LocalRes.getIntString("title.editor"), 0, 3) == 0))
      refreshCurrent();
  }

  public boolean actionClosingForm(Component paramComponent)
    throws BasicException
  {
    if (this.m_Dirty.isDirty())
    {
      int i = JOptionPane.showConfirmDialog(paramComponent, LocalRes.getIntString("message.wannasave"), LocalRes.getIntString("title.editor"), 1, 3);
      if (i == 0)
      {
        saveData();
        return true;
      }
      if (i == 1)
      {
        refreshCurrent();
        return true;
      }
      return false;
    }
    return true;
  }

  public final void actionLoad()
    throws BasicException
  {
    loadData();
    if (this.m_bd.getSize() == 0)
      actionInsert();
  }

  public final void actionInsert()
    throws BasicException
  {
    saveData();
    if (canInsertData())
    {
      this.m_iState = 3;
     try {
    	   this.m_editorrecord.writeValueInsert();
	} catch (Exception e) {
		e.printStackTrace();
	}
   
      this.m_Dirty.setDirty(false);
      fireStateUpdate();
    }
  }

  public final void actionDelete()
    throws BasicException
  {
    saveData();
    if (canDeleteData())
    {
      Object localObject = getCurrentElement();
      int i = getIndex();
      int j = this.m_bd.getSize();
      if ((i >= 0) && (i < j))
      {
        this.m_iState = 2;
        this.m_editorrecord.writeValueDelete(localObject);
        this.m_Dirty.setDirty(true);
        fireStateUpdate();
      }
    }
  }

  private final void baseMoveTo(int paramInt)
  {
    if ((paramInt >= 0) && (paramInt < this.m_bd.getSize()))
      this.m_iIndex = paramInt;
    else
      this.m_iIndex = -1;
    fireDataBrowse();
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.data.user.BrowsableEditableData
 * JD-Core Version:    0.6.0
 */